<template>
  <div>
    <div>
      <el-form  style="display: flex; justify-content: flex-start; margin: 10px 0" :inline="true" :model="exercise" class="demo-form-inline">
        <el-form-item label="名称">
          <el-input v-model="ydmz" placeholder="名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" circle @click="queryAllExercise()"
            ></el-button
          >
        </el-form-item>
      </el-form>
      <el-button
       style="display: flex; justify-content: flex-start; margin: 10px 0"
        @click="
          dialogFormVisible = true;
          exercise = {};sign='add';
          tit='新增运动项目'
        "
        type="primary"
        >新增</el-button
      >
    </div>
    <div>
      <el-table :data="exerciseList" @selection-change="handleSelectionChange" height="65vh">
        <el-table-column prop="exerciseid" type="selection" >
        </el-table-column>
        <el-table-column prop="exerciseid" label="编号">
        </el-table-column>
        <el-table-column prop="ydmz" label="名称" >
        </el-table-column>
        <el-table-column prop="ydqd" label="运动强度" >
        </el-table-column>
        <el-table-column prop="qbdl" label="千步当量" >
        </el-table-column>
        <el-table-column prop="ydsj" label="运动时间" >
        </el-table-column>
        <el-table-column prop="nlqj" label="年龄区间" >
        </el-table-column>
        <el-table-column prop="exerciseid" label="操作">
          <template slot-scope="scope">
            <el-button circle type="primary" @click="dialogFormVisible = true;sign='update';exercise=scope.row;tit='修改运动项目信息'" icon="el-icon-edit"></el-button>
            <el-button
              @click="DeleteExe(scope.row.exerciseid)"
              type="danger"
              icon="el-icon-delete"
              circle
              ></el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[2, 3, 4, 6, 10]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>

    <el-dialog :title="tit" :visible.sync="dialogFormVisible">
      <el-form :model="exercise">
        <el-form-item label="活动名称" :label-width="formLabelWidth">
          <el-input v-model="exercise.ydmz" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="运动强度" :label-width="formLabelWidth">
          <el-select v-model="exercise.ydqd" placeholder="请选择运动强度">
            <el-option label="中" value="中"></el-option>
            <el-option label="强" value="强"></el-option>
            <el-option label="弱" value="弱"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="千步当量" :label-width="formLabelWidth">
          <el-input v-model="exercise.qbdl" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="运动时间" :label-width="formLabelWidth">
          <el-input v-model="exercise.ydsj" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="年龄区间" :label-width="formLabelWidth">
          <el-input v-model="exercise.nlqj" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="
            dialogFormVisible = false;
            add();
          "
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tit:"",
      sign: "add",
      dialogTableVisible: false,
      dialogFormVisible: false,
      exercise: {
        ydmz: "",
        ydqd: "",
        qbdl: 0,
        ydsj: 0,
        nlqj: "",
      },
      ydmz: "",
      currentPage: 1,
      total: 3,
      pageSize: 4,
      exerciseList: [],
      formLabelWidth: "120px",
    };
  },
  created() {
    this.queryAllExercise();
  },
  methods: {
    add() {
      if(this.sign=="add"){
        console.log(this.exercise);
      this.axios.post("/api/lzh/addExercise", this.exercise).then((result) => {
        this.$message({
          message: "添加成功",
          type: "success",
        });
        this.queryAllExercise();
      });
      }else{
        console.log(this.exercise);
      this.axios.post("/api/lzh/UpdateExercise", this.exercise).then((result) => {
        this.$message({
          message: "修改成功",
          type: "success",
        });
        this.queryAllExercise();
      });
      }
    
    },
    DeleteExe(data) {
      console.log(data);
      this.axios.post("/api/lzh/deleteExercise?id="+data).then(result=>{
        this.$message({
          message: "删除成功",
          type: "success",
        });
        this.queryAllExercise();
      })
    },
    queryAllExercise() {
      this.axios
        .post("/api/lzh/queryAllExercise", {
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          searchObj: {
            ydmz: this.ydmz,
          },
        })
        .then((result) => {
          this.total = result.data.total;
          this.exerciseList = result.data.list;
        });
    },
    handleSelectionChange(val) {
      // console.log(val)
      this.multipleSelection = val;
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.queryAllExercise();
      // console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.queryAllExercise();
      // console.log(`当前页: ${val}`);
    },
  },
};
</script>

<style>
</style>